#include "qrcodetools.h"
#include "common/modelindicator.h"
#include "common/versions.h"
#include "generate/qrcodecreator.h"
#include "common/stringutils.h"
#include "decoder/decoder.h"

#include <string>
#include <vector>

#include <QString>

#include <QDebug>

namespace qrcode{

QRCodeTools::QRCodeTools()
{

}

std::string QRCodeTools::GetModelIndicator(std::string &msg)
{
    ModelIndicator *pModelIndicator = new ModelIndicator();
    if(pModelIndicator->AnalysisData(msg))
    {
        std::string modelIndi = pModelIndicator->GetModelIndicator();
        StringUtils::PRINT("模式指示符："+QString::fromStdString(modelIndi));
        return modelIndi;
    }
    else
    {
        qDebug() << "输入信息有误！";
    }
}

bool QRCodeTools::GenerteQRCode(std::string msg, char errorLevel)
{
    std::string modelIndicator = GetModelIndicator(msg);
    Versions *qVersions = new Versions();
    if(qVersions->IsWithinCapacity(msg.length(), modelIndicator, errorLevel)){
        QRCodeCreator *pQRCodeCreator = new QRCodeCreator(msg, errorLevel, modelIndicator);
        pQRCodeCreator->setPic(pic);
        if(pQRCodeCreator->Create()){
            return true;
        } else {
            return false;
        }
    } else {
        qDebug() << "without capacity";
        return false;
    }
}

std::string QRCodeTools::DecodeQRCode(std::__cxx11::string file)
{
    StringUtils::PRINT("-----二维码解码-----");
    Decoder *pdecoder = new Decoder();
    pdecoder->setImagefile(file);
    pdecoder->Decoding();

}

void QRCodeTools::setCOE(int value)
{
    pic.setCOE(value);
}

void QRCodeTools::setFilepath(const std::string &value)
{
    pic.setFilepath(value);
}

void QRCodeTools::setFilename(const std::string &value)
{
    pic.setFilename(value);
}

void QRCodeTools::setPicFormat(const std::string &value)
{
    pic.setPicFormat(value);
}

}
